#!/bin/bash
#
# nosvideo.com module
# Copyright (c) 2010-2012 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.

MODULE_NOSVIDEO_REGEXP_URL="http://\(www\.\)\?nosvideo\.com/*"

MODULE_NOSVIDEO_DOWNLOAD_OPTIONS=""
MODULE_NOSVIDEO_DOWNLOAD_RESUME=yes
MODULE_NOSVIDEO_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=yes

MODULE_NOSVIDEO_LIST_OPTIONS=""

nosvideo_download() {

local COOKIEFILE=$1
local URL=$2

pagetest=$(curl $URL)

if match 'File Not Found' "$pagetest" ; then
  return $ERR_LINK_DEAD
fi
	Pageinit=$(curl "http://nosupload.com/?d=${URL:23}") || return

	fname=$(echo "$Pageinit" | parse_form_input_by_name 'fname')
	
	Pageauth=$(curl -e $URL -d "op=download1&id=${URL:23}&fname=$fname&referer=$URL&method_free=Download+File" "http://nosupload.com/?d=${URL:23}" | break_html_lines_alt ) || return
	
 # Pageauth=$(curl -e $URL -d "op=download1&id=${URL:23}&fname=$fname&referer=$URL&method_free=Download+File" "http://nosupload.com/?d=${URL:23}" | break_html_lines_alt )

  rand=$(echo "$Pageauth" | parse_form_input_by_name 'rand')
  
  wait 30 seconds
  
  Pageauth=$(curl -e $URL -d "btn_download=true&op=download2&id=${URL:23}&rand=$rand&referer=$URL&method_free=Free+Download&down_script=1" "http://nosupload.com/?d=${URL:23}" | break_html_lines_alt )
  
  FILE_URL=$(echo "$Pageauth" | grep 'select' | tail -n1)
  FILE_URL=${FILE_URL:24}
  FILE_URL=${FILE_URL%*\">}
  
  FILENAME="$fname"
	
	echo "$FILE_URL"
	echo "$FILENAME"
}
